package org.dtrd.config.wechat.api;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import org.dtrd.config.wechat.entity.bean.AccessToken;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信各平台通用API
 *
 * @author xiao
 */
public interface IWechatBaseApi {
    /**
     * 微信返回成功的 code
     */
    Integer WECHAT_RESPONSE_SUCCESS = 0;

    String ACCESS_TOKEN = "ACCESS_TOKEN";
    String GRANT_TYPE = "grant_type";
    String JS_CODE = "js_code";
    String APPID = "APPID";
    String OPENID = "OPENID";
    String SECRET = "SECRET";
    String CODE = "CODE";
    String REFRESH_TOKEN = "REFRESH_TOKEN";

    /**
     * 微信错误码
     */
    String ERRCODE = "errcode";
    /**
     * 微信错误信息
     */
    String ERRMSG = "errmsg";

    /**
     * 生成码个数总和到达最大个数限制
     */
    int ERROR_CODE_REACHED_LIMIT = 45029;

    /**
     * 获取微信token链接
     */
    String ACCESSTOKEN_PATH = "https://api.weixin.qq.com/cgi-bin/token";

    default AccessToken getAccessToken(String appid, String secret) {
        AccessToken accessToken = null;
        Map<String, Object> map = generateBaseParams(appid, secret);
        map.put(GRANT_TYPE, "client_credential");
        String s = doGet(ACCESSTOKEN_PATH, map);
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println("IWechatBaseApi.getAccessToken appid, secret]jsonObject= " + jsonObject);
        if (null != jsonObject) {
            try {
                accessToken = new AccessToken();
                accessToken.setAccessToken(jsonObject.getString("access_token"));
                accessToken.setExpiresIn(jsonObject.getIntValue("expires_in"));
            } catch (Exception var8) {
                accessToken = null;
                int errcode = jsonObject.getIntValue("errcode");
                String errmsg = jsonObject.getString("errmsg");
            }
        }
        return accessToken;
    }

    /**
     * 生成基础请求信息
     *
     * @param appid
     * @param secret
     * @return
     */
    default Map<String, Object> generateBaseParams(String appid, String secret) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("appid", appid);
        map.put("secret", secret);
        return map;
    }

    /**
     * 替换链接中AccessToken
     *
     * @param url
     * @param accessToken
     * @return
     */
    default String replaceAccessToken(String url, String accessToken) {
        return url.replace(ACCESS_TOKEN, accessToken);
    }

    /**
     * 执行Get请求
     *
     * @param url
     * @param map
     * @return
     */
    default String doGet(String url, Map map) {
        return HttpUtil.get(url, map);
    }

    default String doGet(String url, String accessToken) {
        return HttpUtil.get(replaceAccessToken(url, accessToken));
    }

    /**
     * 执行POST请求
     *
     * @param url
     * @param body
     * @return
     */
    default <T> String doPost(String url, T body) {
        return HttpUtil.post(url, JSONUtil.toJsonStr(body));
    }

    /**
     * 执行POST请求
     *
     * @param url
     * @param accessToken
     * @param body
     * @param <T>
     * @return
     */
    default <T> String doPost(String url, String accessToken, T body) {
        System.out.println("IWechatBaseApi.doPost urk=" + replaceAccessToken(url, accessToken) + "; JSONUtil.toJsonStr(body)=" + JSONUtil.toJsonStr(body));
        return HttpUtil.post(replaceAccessToken(url, accessToken), JSONUtil.toJsonStr(body));
    }

    /**
     * 执行POST请求并返回原始Response
     *
     * @param url
     * @param accessToken
     * @param body
     * @param <T>
     * @return
     */
    default <T> HttpResponse doPostForHttpResponse(String url, String accessToken, T body) {
        return HttpRequest.post(replaceAccessToken(url, accessToken)).timeout(-1).body(JSONUtil.toJsonStr(body)).execute();
    }

    /**
     * 是否反馈成功
     *
     * @param json
     * @return
     */
    default boolean isSuccess(JSONObject json) {
        return json.getIntValue(ERRCODE) == WECHAT_RESPONSE_SUCCESS;
    }

}
